package data;

import util.ExportFile;
import util.ImportFile;
import util.Transform;

public class DataGEN {
    private Markers m_data;
    private Segments s_data;
    private Genes g_data;

    public Genes getG_data() {
        return g_data;
    }

    public void setG_data(Genes g_data) {
        this.g_data = g_data;
    }

    public Markers getM_data() {
        return m_data;
    }

    public void setM_data(Markers m_data) {
        this.m_data = m_data;
    }

    public Segments getS_data() {
        return s_data;
    }

    public void setS_data(Segments s_data) {
        this.s_data = s_data;
    }

    public DataGEN(Markers m_data, Segments s_data,Genes g_data) {
        this.m_data = m_data;
        this.s_data = s_data;
        this.g_data = g_data;
    }

    public DataGEN(Segments s_data,Genes g_data) {
        this.s_data = s_data;
        this.g_data = g_data;
    }

    public DataGEN(Markers m_data, Genes g_data) {
        this.m_data = m_data;
        this.g_data = g_data;
    }

    public DataGEN(Reader reader){
        int markersNum = Integer.valueOf(reader.getMarkersNum());
        int chromsomeNum = Integer.valueOf(reader.getChromsomeNum());
        int genesNum = Integer.valueOf(reader.getGenesNum());
        int posMin = Integer.valueOf(reader.getPosMin());
        int posMax = Integer.valueOf(reader.getPosMax());
        int samplesNum = Integer.valueOf(reader.getSamplesNum());
        int passengersNum = Integer.valueOf(reader.getPassengersNum());
        int passengersWidth = Integer.valueOf(reader.getPassengersNum());
        double passengersVal = Double.valueOf(reader.getPassengersVal());
        boolean passengersflag = Boolean.valueOf(reader.getPassengersFlag());
        int[] driversMidPos = Transform.toIntArray(reader.getDriversMidPos());
        int[] driversWidth = Transform.toIntArray(reader.getDriversWidth());
        double[] driversVal = Transform.toDoubleArray(reader.getDriversVal());
        double alpha = Double.valueOf(reader.getAlpha());
        double beta = Double.valueOf(reader.getBeta());
        double sigma = Double.valueOf(reader.getSigma());


        Passengers passengers = new Passengers(passengersNum,passengersWidth,passengersVal,passengersflag);

        Window window = new Window(driversMidPos,driversWidth,driversVal,alpha,beta,sigma);

        Markers markers = new Markers();
        markers.simulateEqually("P000000",chromsomeNum,markersNum,posMin,posMax);

        Segments segments = new Segments();
        segments.simulate("S0000",samplesNum,passengers,markers,window);

        Genes genes = new Genes();
        genes.simulateEqually("G0000",chromsomeNum,genesNum,posMin,posMax);

        this.m_data = markers;
        this.s_data = segments;
        this.g_data = genes;
    }

    //提取RUBIC文件的数据
    public void LoadRUBIC(){
        m_data.assign(Constant.IMPORT_PATH_RUBIC_MARKERS,"\t",true);
        s_data.assign(Constant.IMPORT_PATH_RUBIC_SEGCNA,"\t",false);
        g_data.assign(Constant.IMPORT_PATH_RUBIC_GENES,"\t",true);
    }

    //提取GISTIC1.0文件的数据
    public void LoadGISTIC1(){
//        m_data.assign(Constant.IMPORT_PATH_RUBIC_MARKERS,"\t",true);
//        s_data.assign(Constant.IMPORT_PATH_RUBIC_SEGCNA,"\t",false);
//        g_data.assign(Constant.IMPORT_PATH_RUBIC_GENES,"\t",true);
    }

    //提取GISTIC2.0文件的数据
    public void LoadGISTIC2(){
//        m_data.assign(Constant.IMPORT_PATH_RUBIC_MARKERS,"\t",true);
//        s_data.assign(Constant.IMPORT_PATH_RUBIC_SEGCNA,"\t",false);
//        g_data.assign(Constant.IMPORT_PATH_RUBIC_GENES,"\t",true);
    }

    //提取JISTIC文件的数据
    public void LoadJISTIC(){
//        m_data.assign(Constant.IMPORT_PATH_RUBIC_MARKERS,"\t",true);
//        s_data.assign(Constant.IMPORT_PATH_RUBIC_SEGCNA,"\t",false);
//        g_data.assign(Constant.IMPORT_PATH_RUBIC_GENES,"\t",true);
    }

    //提取SAIC文件的数据
    public void LoadSAIC(){
//        m_data.assign(Constant.IMPORT_PATH_RUBIC_MARKERS,"\t",true);
//        s_data.assign(Constant.IMPORT_PATH_RUBIC_SEGCNA,"\t",false);
//        g_data.assign(Constant.IMPORT_PATH_RUBIC_GENES,"\t",true);
    }

    //改变现实数据
    public void changeValue(double amp_th,double del_th,double amp_overValue,double del_overValue,double amp_underValue,double del_underValue){
        double[] log_ratio= s_data.getLog_ratio();
        int log_ratio_length = log_ratio.length;

        for (int i=0 ;i<log_ratio_length;i++){
            if(log_ratio[i]>=amp_th){
                log_ratio[i] = amp_overValue;
            }else if (log_ratio[i]<=-del_th){
                log_ratio[i] = del_overValue;
            }else if (log_ratio[i]<amp_th && log_ratio[i]>0){
                log_ratio[i] = amp_underValue;
            }else if (log_ratio[i]<0 && log_ratio[i]>del_th){
                log_ratio[i] = del_underValue;
            }else {
                log_ratio[i] = 0;
            }
        }
    }

    //输出RUBIC数据文件
    public void saveRUBIC(){
        int m_data_length = m_data.getName().length;
        int s_data_length = s_data.getSample().length;
        int g_data_length = g_data.getAssociated_gene_name().length;
        String[][] m_exportData = new String[3][m_data_length];
        String[][] s_exportData = new String[6][s_data_length];
        String[][] g_exportData = new String[5][g_data_length];

        //将类里的值赋值
        m_exportData[0] = m_data.getName();
        m_exportData[1] = m_data.getChromosome();
        m_exportData[2] = Transform.toStringArray(m_data.getPosition());

        s_exportData[0] = s_data.getSample();
        s_exportData[1] = s_data.getChromosome();
        s_exportData[2] = Transform.toStringArray(s_data.getStart());
        s_exportData[3] = Transform.toStringArray(s_data.getEnd());
        s_exportData[4] = Transform.toStringArray(s_data.getCountsOfProbes());
        s_exportData[5] = Transform.toStringArray(s_data.getLog_ratio());

        g_exportData[0] = g_data.getEnsembl_gene_id();
        g_exportData[1] = g_data.getAssociated_gene_name();
        g_exportData[2] = Transform.toStringArray(g_data.getStart());
        g_exportData[3] = Transform.toStringArray(g_data.getEnd());
        g_exportData[4] = g_data.getChromosome();

        //根据该算法自带的数据列名而来，有些数据不存在列名
        String[] m_colName = {"name", "chrom", "pos"};
        String[] g_colName = {"Ensembl Gene ID", "Associated Gene Name", "Gene Start (bp)","Gene End (bp)","Chromosome Name"};

        //写出文件
        ExportFile.writeTableFile(Constant.EXPORT_PATH_RUBIC_SEGCNA , null , Transform.transpose(s_exportData));
        ExportFile.writeTableFile(Constant.EXPORT_PATH_RUBIC_MARKERS , m_colName , Transform.transpose(m_exportData));
        ExportFile.writeTableFile(Constant.EXPORT_PATH_RUBIC_GENES , g_colName , Transform.transpose(g_exportData));
    }

    //输出GISTIC1.0数据文件，缺一个文件cytoBand（大分区）
    public void saveGISTIC1(){
        int m_data_length = m_data.getName().length;  //可不要
        int s_data_length = s_data.getSample().length;
        int g_data_length = g_data.getAssociated_gene_name().length;
        String[][] m_exportData = new String[3][m_data_length];
        String[][] s_exportData = new String[6][s_data_length];
        String[][] g_exportData = new String[9][g_data_length]; //这里需要9行
        String[][] g_exportData2 = new String[5][g_data.getG_detail().chromosomeNumber]; //这里需要5行，把某一种染色体分为一个大区（简化）

        //将类里的值赋值
        m_exportData[0] = m_data.getName();
        m_exportData[1] = m_data.getChromosome();
        m_exportData[2] = Transform.toStringArray(m_data.getPosition());

        s_exportData[0] = s_data.getSample();
        s_exportData[1] = s_data.getChromosome();
        s_exportData[2] = Transform.toStringArray(s_data.getStart());
        s_exportData[3] = Transform.toStringArray(s_data.getEnd());
        s_exportData[4] = Transform.toStringArray(s_data.getCountsOfProbes());
        s_exportData[5] = Transform.toStringArray(s_data.getLog_ratio());

        g_exportData[0] = Transform.toStringArray(g_data.getOrder());
        g_exportData[1] = g_data.getAssociated_gene_name(); //这里的基因名，都用流水号代替
        g_exportData[2] = g_data.getAssociated_gene_name();  //不知道是啥，所以用流水号代替
        g_exportData[3] = g_data.getChromosome();
        g_exportData[4] = g_data.getAssociated_gene_name(); //不知道是啥，所以用流水号代替
        g_exportData[5] = Transform.toStringArray(g_data.getStart());
        g_exportData[6] = Transform.toStringArray(g_data.getEnd());
        g_exportData[7] = g_data.getAssociated_gene_name(); //不知道是啥，所以用流水号代替
        g_exportData[8] = g_data.getAssociated_gene_name(); //不知道是啥，所以用流水号代替

        g_exportData2[0] = g_data.getG_detail().chromosome_bigZone;
        g_exportData2[1] = Transform.toStringArray(g_data.getG_detail().start_bigZone);
        g_exportData2[2] = Transform.toStringArray(g_data.getG_detail().end_bigZone);
        g_exportData2[3] = g_data.getG_detail().name_bigZone; //不知道是啥，所以用流水号代替
        g_exportData2[4] = g_data.getG_detail().name_bigZone; //不知道是啥，所以用流水号代替

        //根据该算法自带的数据列名而来，有些数据不存在列名，这里不是gistic2.0的列名，其列名在mat文件中
        String[] s_colName = {"Sample", "Chromosome", "Start","End","Probe_Number","Segment_Mean"};
        String[] m_colName = {"probe", "chrom", "loc"};


        //写出文件
        ExportFile.writeTableFile(Constant.EXPORT_PATH_GISTIC1_SEGMENT , s_colName , Transform.transpose(s_exportData));
        ExportFile.writeTableFile(Constant.EXPORT_PATH_GISTIC1_PROBE_LOCATIONS , m_colName , Transform.transpose(m_exportData));
        ExportFile.writeTableFile(Constant.EXPORT_PATH_GISTIC1_GENES , null , Transform.transpose(g_exportData));
        ExportFile.writeTableFile(Constant.EXPORT_PATH_GISTIC1_CYtOBAND , null , Transform.transpose(g_exportData2));

    }

    //输出GISTIC2.0数据文件，需要改下列名
    public void saveGISTIC2(){
        int m_data_length = m_data.getName().length;  //可不要
        int s_data_length = s_data.getSample().length;
        int g_data_length = g_data.getAssociated_gene_name().length;
        String[][] m_exportData = new String[3][m_data_length];
        String[][] s_exportData = new String[6][s_data_length];
        String[][] g_exportData = new String[5][g_data_length];

        //将类里的值赋值
        m_exportData[0] = m_data.getName();
        m_exportData[1] = m_data.getChromosome();
        m_exportData[2] = Transform.toStringArray(m_data.getPosition());

        s_exportData[0] = s_data.getSample();
        s_exportData[1] = s_data.getChromosome();
        s_exportData[2] = Transform.toStringArray(s_data.getStart());
        s_exportData[3] = Transform.toStringArray(s_data.getEnd());
        s_exportData[4] = Transform.toStringArray(s_data.getCountsOfProbes());
        s_exportData[5] = Transform.toStringArray(s_data.getLog_ratio());

        g_exportData[0] = g_data.getEnsembl_gene_id();
        g_exportData[1] = g_data.getAssociated_gene_name();
        g_exportData[2] = Transform.toStringArray(g_data.getStart());
        g_exportData[3] = Transform.toStringArray(g_data.getEnd());
        g_exportData[4] = g_data.getChromosome();

        //根据该算法自带的数据列名而来，有些数据不存在列名，这里不是gistic2.0的列名，其列名在mat文件中
        String[] g_colName = {"Ensembl Gene ID", "Associated Gene Name", "Gene Start (bp)","Gene End (bp)","Chromosome Name"};

        //写出文件
        ExportFile.writeTableFile(Constant.EXPORT_PATH_GISTIC2_SEGMENTATIONFILE , null , Transform.transpose(s_exportData));
        ExportFile.writeTableFile(Constant.EXPORT_PATH_GISTIC2_MARKERSFILE , null , Transform.transpose(m_exportData));
        ExportFile.writeTableFile(Constant.EXPORT_PATH_GISTIC2_REFGENEFILE , g_colName , Transform.transpose(g_exportData));
    }

    //输出JISTIC数据文件
    public void saveJISTIC(){
        int m_data_length = m_data.getName().length;  //可不要
        int s_data_length = s_data.getSample().length;
        int g_data_length = g_data.getAssociated_gene_name().length;
        String[][] m_exportData = new String[3][m_data_length];
        String[][] s_exportData = new String[6][s_data_length];
        String[][] g_exportData = new String[9][g_data_length]; //这里需要9列

        //将类里的值赋值
        m_exportData[0] = m_data.getName();
        m_exportData[1] = m_data.getChromosome();
        m_exportData[2] = Transform.toStringArray(m_data.getPosition());

        s_exportData[0] = s_data.getSample();
        s_exportData[1] = s_data.getChromosome();
        s_exportData[2] = Transform.toStringArray(s_data.getStart());
        s_exportData[3] = Transform.toStringArray(s_data.getEnd());
        s_exportData[4] = Transform.toStringArray(s_data.getCountsOfProbes());
        s_exportData[5] = Transform.toStringArray(s_data.getLog_ratio());

        g_exportData[0] = Transform.toStringArray(g_data.getOrder());
        g_exportData[1] = g_data.getAssociated_gene_name(); //这里的基因名，都用流水号代替
        g_exportData[2] = g_data.getAssociated_gene_name();  //不知道是啥，所以用流水号代替
        g_exportData[3] = g_data.getChromosome();
        g_exportData[4] = g_data.getAssociated_gene_name(); //不知道是啥，所以用流水号代替
        g_exportData[5] = Transform.toStringArray(g_data.getStart());
        g_exportData[6] = Transform.toStringArray(g_data.getEnd());
        g_exportData[7] = g_data.getAssociated_gene_name(); //不知道是啥，所以用流水号代替
        g_exportData[8] = g_data.getAssociated_gene_name(); //不知道是啥，所以用流水号代替


        //根据该算法自带的数据列名而来，有些数据不存在列名，这里不是gistic2.0的列名，其列名在mat文件中
        String[] s_colName = {"Sample", "Chromosome", "Start","End","Probe_Number","Segment_Mean"};
        String[] m_colName = {"probe", "chrom", "loc"};


        //写出文件
        ExportFile.writeTableFile(Constant.EXPORT_PATH_JISTIC_SEGMENT , s_colName , Transform.transpose(s_exportData));
        ExportFile.writeTableFile(Constant.EXPORT_PATH_JISTIC_PROBE_LOCATIONS , m_colName , Transform.transpose(m_exportData));
        ExportFile.writeTableFile(Constant.EXPORT_PATH_JISTIC_GENES , null , Transform.transpose(g_exportData));

    }

    //输出SAIC数据文件，貌似有点不对
    public void saveSAIC(){
        int m_data_length = m_data.getName().length;
        int g_data_length = g_data.getAssociated_gene_name().length;
        String[][] m_exportData = new String[3+s_data.getLog_ratio_everyPoint().length][m_data_length];
        String[][] g_exportData = new String[5][g_data_length];

        int value = s_data.getLog_ratio_everyPoint().length;


        //将类里的值赋值
        m_exportData[0] = m_data.getChromosome();
        m_exportData[1] = m_data.getName();
        m_exportData[2] = Transform.toStringArray(m_data.getPosition());
        for (int i=3;i<3+s_data.getLog_ratio_everyPoint().length;i++){
            m_exportData[i] = Transform.toStringArray(s_data.getLog_ratio_everyPoint()[i-3]);
        }


        g_exportData[0] = g_data.getChromosome();
        g_exportData[1] = Transform.toStringArray(g_data.getStart());
        g_exportData[2] = Transform.toStringArray(g_data.getEnd());
        g_exportData[3] = Transform.toStringArray(g_data.getOrder());
        g_exportData[4] = g_data.getAssociated_gene_name();

        //根据该算法自带的数据列名而来，有些数据不存在列名
        String[] g_colName = {"chrom", "start","end","geneid","genename"};

        //写出文件
        ExportFile.writeTableFile(Constant.EXPORT_PATH_SAIC_MARKERS , null , Transform.transpose(m_exportData));
        ExportFile.writeTableFile(Constant.EXPORT_PATH_SAIC_GENE_REFERENCE , g_colName , Transform.transpose(g_exportData));

    }

    //输出几种算法的数据文件
    public void saveAll(){
        this.saveRUBIC();
        this.saveGISTIC1();
        this.saveGISTIC2();
        this.saveJISTIC();
        this.saveSAIC();
    }
}
